xml-rs, an XML library for Rust
xml-rs is an XML library for Rust programming language. It is heavily inspired by Java Streaming API for XML (StAX).
This library currently contains pull parser much like StAX event reader. It provides iterator API, so you can leverage Rust's existing iterators library features.
It also provides a streaming document writer much like StAX event writer. This writer consumes its own set of events, but reader events can be converted to writer events easily, and so it is possible to write XML transformation chains in a pretty clean manner.
This parser is mostly full-featured, however, there are limitations:
- no other encodings but UTF-8 are supported yet, because no stream-based encoding library is available now; when (or if) one will be available, I'll try to make use of it;
- DTD validation is not supported,
<!DOCTYPE>
declarations are completely ignored; thus no support for custom entities too; internal DTD declarations are likely to cause parsing errors; - attribute value normalization is not performed, and end-of-line characters are not normalized too.
Other than that the parser tries to be mostly XML-1.0-compliant.
Writer is also mostly full-featured with the following limitations:
- no support for encodings other than UTF-8, for the same reason as above;
- no support for emitting
<!DOCTYPE>
declarations; - more validations of input are needed, for example, checking that namespace prefixes are bounded or comments are well-formed.
What is planned (highest priority first, approximately):
- missing features required by XML standard (e.g. aforementioned normalization and proper DTD parsing);
- miscellaneous features of the writer;
- parsing into a DOM tree and its serialization back to XML text;
- SAX-like callback-based parser (fairly easy to implement over pull parser);
- DTD validation;
- (let's dream a bit) XML Schema validation.
Building and using
xml-rs uses Cargo, so just add a dependency section in your project's manifest:
[]
= "0.6"
The package exposes a single crate called xml
:
extern crate xml;
Reading XML documents
xml::reader::EventReader
requires a Read
instance to read from. When a proper stream-based encoding
library is available, it is likely that xml-rs will be switched to use whatever character stream structure
this library would provide, but currently it is a Read
.
Using EventReader
is very straightforward. Just provide a Read
instance to obtain an iterator
over events:
extern crate xml;
use File;
use BufReader;
use ;
EventReader
implements IntoIterator
trait, so you can just use it in a for
loop directly.
Document parsing can end normally or with an error. Regardless of exact cause, the parsing
process will be stopped, and iterator will terminate normally.
You can also have finer control over when to pull the next event from the parser using its own
next()
method:
match parser.next
Upon the end of the document or an error the parser will remember that last event and will always
return it in the result of next()
call afterwards. If iterator is used, then it will yield
error or end-of-document event once and will produce None
afterwards.
It is also possible to tweak parsing process a little using xml::reader::ParserConfig
structure.
See its documentation for more information and examples.
You can find a more extensive example of using EventReader
in src/analyze.rs
, which is a
small program (BTW, it is built with cargo build
and can be run after that) which shows various
statistics about specified XML document. It can also be used to check for well-formedness of
XML documents - if a document is not well-formed, this program will exit with an error.
Writing XML documents
xml-rs also provides a streaming writer much like StAX event writer. With it you can write an
XML document to any Write
implementor.
extern crate xml;
use File;
use ;
use ;
The code example above also demonstrates how to create a writer out of its configuration.
Similar thing also works with EventReader
.
The library provides an XML event building DSL which helps to construct complex events, e.g. ones having namespace definitions. Some examples:
// <a:hello a:param="value" xmlns:a="urn:some:document">
start_element.attr.ns
// <hello b:config="name" xmlns="urn:default:uri">
start_element.attr.default_ns
// <![CDATA[some unescaped text]]>
cdata
Of course, one can create XmlEvent
enum variants directly instead of using the builder DSL.
There are more examples in xml::writer::XmlEvent
documentation.
The writer has multiple configuration options; see EmitterConfig
documentation for more
information.
Other things
No performance tests or measurements are done. The implementation is rather naive, and no specific optimizations are made. Hopefully the library is sufficiently fast to process documents of common size. I intend to add benchmarks in future, but not until more important features are added.
Known issues
All known issues are present on GitHub issue tracker: http://github.com/netvl/xml-rs/issues. Feel free to post any found problems there.
License
This library is licensed under MIT license.
Copyright (C) Vladimir Matveev, 2014-2017